package com.chen.springboot.controller;

import com.chen.springboot.common.AuthAccess;
import com.chen.springboot.common.Result;
import com.chen.springboot.entity.Order;
import com.chen.springboot.service.AiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AI助手控制器
 */
@RestController
@RequestMapping("/ai")
public class AiAssistantController {

    @Autowired
    private AiService aiService;

    /**
     * AI助手对话接口
     */
    @AuthAccess
    @PostMapping("/assistant")
    public Result<Map<String, String>> assistantChat(@RequestBody Map<String, Object> request) {
        try {
            String query = (String) request.get("query");
            
            if (query == null || query.trim().isEmpty()) {
                return Result.error("400", "查询内容不能为空");
            }

            // 调用AI服务进行对话
            String answer = aiService.chatWithAI(query);
            
            Map<String, String> resultData = new HashMap<>();
            resultData.put("answer", answer);
            
            return Result.success(resultData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "AI助手服务异常：" + e.getMessage());
        }
    }
    
    /**
     * 获取热门商品推荐
     */
    @AuthAccess
    @GetMapping("/recommend/hot")
    public Result<Map<String, Object>> getHotRecommendations() {
        try {
            Map<String, Object> recommendations = aiService.getHotRecommendations();
            return Result.success(recommendations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "获取推荐失败：" + e.getMessage());
        }
    }
    
    /**
     * 商品价格对比
     */
    @AuthAccess
    @PostMapping("/compare/price")
    public Result<Map<String, Object>> comparePrices(@RequestBody Map<String, Object> request) {
        try {
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> products = (List<Map<String, Object>>) request.get("products");
            
            if (products == null || products.isEmpty()) {
                return Result.error("400", "商品列表不能为空");
            }

            Map<String, Object> comparisonResult = aiService.comparePrices(products);
            return Result.success(comparisonResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "价格对比失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户订单信息
     */
    @GetMapping("/user/orders")
    public Result<List<Order>> getUserOrders() {
        try {
            List<Order> orders = aiService.getUserOrders();
            return Result.success(orders);
        } catch (Exception e) {
            e.printStackTrace();
            // 检查是否因为未登录导致的错误
            if (e.getMessage().contains("用户未登录")) {
                return Result.error("401", "请先登录后再查询订单信息");
            }
            return Result.error("500", "获取订单信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户购物车内容
     */
    @GetMapping("/user/cart")
    public Result<List<Order>> getUserCart() {
        try {
            List<Order> cartItems = aiService.getUserCart();
            return Result.success(cartItems);
        } catch (Exception e) {
            e.printStackTrace();
            // 检查是否因为未登录导致的错误
            if (e.getMessage().contains("用户未登录")) {
                return Result.error("401", "请先登录后再查询购物车信息");
            }
            return Result.error("500", "获取购物车信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户发货信息
     */
    @GetMapping("/user/shipping")
    public Result<List<Order>> getUserShippingInfo() {
        try {
            List<Order> shippingInfo = aiService.getUserShippingInfo();
            return Result.success(shippingInfo);
        } catch (Exception e) {
            e.printStackTrace();
            // 检查是否因为未登录导致的错误
            if (e.getMessage().contains("用户未登录")) {
                return Result.error("401", "请先登录后再查询发货信息");
            }
            return Result.error("500", "获取发货信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户退款信息
     */
    @GetMapping("/user/refund")
    public Result<List<Order>> getUserRefundInfo() {
        try {
            List<Order> refundInfo = aiService.getUserRefundInfo();
            return Result.success(refundInfo);
        } catch (Exception e) {
            e.printStackTrace();
            // 检查是否因为未登录导致的错误
            if (e.getMessage().contains("用户未登录")) {
                return Result.error("401", "请先登录后再查询退款信息");
            }
            return Result.error("500", "获取退款信息失败：" + e.getMessage());
        }
    }
}